package jh.com.jhappbf.dispatchcar;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;

import com.ab.activity.AbActivity;
import com.ab.fragment.AbDialogFragment;
import com.ab.util.AbToastUtil;
import com.ab.view.titlebar.AbTitleBar;
import com.yzq.zxinglibrary.common.Constant;

import java.util.ArrayList;

import jh.com.jhappbf.R;
import jh.com.jhappbf.model.PihaoModel;
import jh.com.jhappbf.weight.query.SelectActivity;

/**
 * @author Administrator
 */
public class ContractSelectPihao extends AbActivity{
    private View mDataContentView;
    private ArrayList<PihaoModel> pihaoModels;

    private String companyCode;
    private String cargonameId;
    private String danhao;
    private String hanghao;
    private String danwei;
    private String mType;
    private String initGross;
    private String flag;
    private TextView cargonameIdTxt;
    private TextView companyCodeTxt;
    private TextView initGrossTxt;
    private EditText piciExt;
    private EditText libExt;
    private EditText numExt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDataContentView = View.inflate(ContractSelectPihao.this, R.layout.list_view_item_txt_check_lib2, null);
        setAbContentView(mDataContentView);
        AbTitleBar mAbTitleBar = this.getTitleBar();
        companyCode=getIntent().getStringExtra("CompanyCode");
        cargonameId=getIntent().getStringExtra("cargonameId");
        danhao=getIntent().getStringExtra("danhao");
        hanghao=getIntent().getStringExtra("hanghao");
        danwei=getIntent().getStringExtra("danwei");
        initGross=getIntent().getStringExtra("initGross");
        mType=getIntent().getStringExtra("mType");
        flag=getIntent().getStringExtra("flag");
        pihaoModels= (ArrayList<PihaoModel>) getIntent().getSerializableExtra("PihaoModels");

        mAbTitleBar.setTitleText("信息填写");
        mAbTitleBar.setLogo(R.drawable.button_selector_back);
        mAbTitleBar.setTitleBarBackgroundColor(ContextCompat.getColor(this, R.color.orange));
        mAbTitleBar.setTitleTextMargin(10, 0, 0, 0);
        mAbTitleBar.setLogoLine(R.mipmap.line);
        mAbTitleBar.setLogoOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        View rightViewMore = mInflater.inflate(R.layout.title_right_ok_btn_view, null);
        mAbTitleBar.addRightView(rightViewMore);
        Button mTitleRightBtn = rightViewMore.findViewById(R.id.title_right_ok_btn);
        mTitleRightBtn.setOnClickListener(v -> {
            if (TextUtils.isEmpty(libExt.getText().toString())||TextUtils.isEmpty(piciExt.getText().toString())){
                AbToastUtil.showToast(this,"请填写信息后提交！");
                return;
            }
            confirmSelect();
        });
        initView();
    }
    /**
     * 保存
     */
    private void confirmSelect() {
        if (TextUtils.isEmpty(numExt.getText().toString())){
            numExt.setText("0");
        }
        Double allNum=Double.parseDouble(numExt.getText().toString());
        Double initGrossD=Double.parseDouble(initGross);
        if ((allNum-initGrossD>0)&&!TextUtils.isEmpty(flag)&&!TextUtils.equals("采购",mType)){
            AbToastUtil.showToast(this,"确认数量大于订单数量");
        }else {
            savePici("picihao",piciExt.getText().toString());
            PihaoModel pihaoModel = new PihaoModel();
            pihaoModel.setChk(true);
            pihaoModel.setChk(true);
            pihaoModel.setMATNR(cargonameId);
            pihaoModel.setWERKS(companyCode);
            pihaoModel.setDanhao(danhao);
            pihaoModel.setHanghao(hanghao);
            pihaoModel.setDanwei(danwei);
            pihaoModel.setBy1(mType);
            pihaoModel.setLGORT(libExt.getText().toString());
            pihaoModel.setCHARG(piciExt.getText().toString());
            pihaoModel.setNum(numExt.getText().toString());
            pihaoModels.add(pihaoModel);
            Intent i = new Intent();
            i.putExtra("pihaoModels", pihaoModels);
            setResult(3, i);

            this.finish();
        }

    }

    /**
     * 初始化界面
     */
    private void initView() {
        cargonameIdTxt = mDataContentView.findViewById(R.id.cargoname_id);
        companyCodeTxt = mDataContentView.findViewById(R.id.company_id);
        piciExt = mDataContentView.findViewById(R.id.list_view_item_pici_ext);
        libExt = mDataContentView.findViewById(R.id.list_view_item_lib_ext);
        numExt = mDataContentView.findViewById(R.id.list_view_item_ext);
        piciExt.setText(getPici());
        libExt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(ContractSelectPihao.this, SelectActivity.class);
                i.putExtra("flag", "unloadArea");
                startActivityForResult(i, 10010);
            }
        });
        LinearLayout ll = mDataContentView.findViewById(R.id.commit_ll);
        if (TextUtils.isEmpty(flag)){
            ll.setVisibility(View.GONE);
        }
        if(TextUtils.equals("销售",mType)){
            numExt.setEnabled(false);
        }
        if (pihaoModels!=null){
            piciExt.setText(pihaoModels.get(0).getCHARG());
            numExt.setText(pihaoModels.get(0).getNum());
            libExt.setText(pihaoModels.get(0).getLGORT());
        }else {

            numExt.setText(initGross);
        }
        initGrossTxt = mDataContentView.findViewById(R.id.init_gross);
        cargonameIdTxt.setText(cargonameId.substring(10));
        companyCodeTxt.setText(companyCode);
        initGrossTxt.setText(initGross);
        pihaoModels=new ArrayList<>();

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode,  Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data == null) {
            return;
        }
        String value = data.getStringExtra("result");
        switch (requestCode) {
            case 10010:
                if (!TextUtils.isEmpty(value)) {
                    libExt.setText(value);
                }
                break;
        }
    }

    @Override
    public void onBackPressed() {
        finish();
    }

    /**
     * 保存批次
     */
    private void savePici(String key, String value) {
        SharedPreferences preferences = getSharedPreferences("caption", MODE_PRIVATE);
        SharedPreferences.Editor editor = preferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    /**
     * 从配置文件中读取指定的配置
     *
     * @return key 对应的值
     */
    private String getPici() {
        SharedPreferences preferences = getSharedPreferences("caption", MODE_PRIVATE);
        return preferences.getString("picihao", "");
    }
}
